package com.doge.walknovel.business.home

import android.content.Context
import android.graphics.Typeface
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.animation.AnimationUtils
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.viewpager2.widget.ViewPager2
import com.doge.wnpro.R
import com.doge.wnpro.databinding.LayoutNavigationBinding
import com.doge.walknovel.utils.setOnClickCallback

class NavigationLayout @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null
) : ConstraintLayout(context, attrs) {
    private val binding =
        LayoutNavigationBinding.inflate(LayoutInflater.from(context), this)

    private val animScaleKick = AnimationUtils.loadAnimation(context, R.anim.anims_scale_kick)!!
    private val itemViews by lazy { arrayOf(binding.tabLibrary, binding.tabDiscover, binding.tabGenres, binding.tabProfile) }
    private val itemIconViews by lazy {
        arrayOf(binding.tabLibraryIcon, binding.tabDiscoverIcon, binding.tabGenresIcon, binding.tabProfileIcon)
    }
    private val itemTextViews by lazy {
        arrayOf(binding.tabLibraryText, binding.tabDiscoverText, binding.tabGenresText, binding.tabProfileText)
    }
    private var currentIndex = -1
    private var pager: ViewPager2? = null
    private var callback: Callback? = null

    interface Callback {
        fun onItemClick(position: Int): Boolean
        fun onItemClickAgain(position: Int)
    }

    init {
        itemViews.forEachIndexed { index, view ->
            view.setOnClickCallback { select(index) }
        }
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        pager?.currentItem?.let(::select)
    }

    private fun select(target: Int) {
        if (currentIndex == target) {
            val animId = when (currentIndex) {
                0 -> R.mipmap.home_tab_library_selected
                1 -> R.mipmap.home_tab_discover_selected
                2 -> R.mipmap.home_tab_genres_selected
                else -> R.mipmap.home_tab_profile_selected
            }
            select(itemIconViews[target], animId, itemTextViews[target])
            callback?.onItemClickAgain(target)
            return
        }
        if (callback?.onItemClick(target) != true) {
            currentIndex = target
            itemViews.forEachIndexed { index, _ ->
                if (target == index) {
                    val animId = when (index) {
                        0 -> R.mipmap.home_tab_library_selected
                        1 -> R.mipmap.home_tab_discover_selected
                        2 -> R.mipmap.home_tab_genres_selected
                        else -> R.mipmap.home_tab_profile_selected
                    }
                    select(itemIconViews[index], animId, itemTextViews[index])
                } else {
                    val restId = when (index) {
                        0 -> R.mipmap.home_tab_library
                        1 -> R.mipmap.home_tab_discover
                        2 -> R.mipmap.home_tab_genres
                        else -> R.mipmap.home_tab_profile
                    }
                    reset(itemIconViews[index], restId, itemTextViews[index])
                }
            }
        }
    }

    fun attach(viewPager: ViewPager2) {
        pager = viewPager
        viewPager.currentItem.let(::select)
    }

    fun setListener(listener: Callback?) {
        callback = listener
    }

    private fun select(imageView: ImageView, animationId: Int, textView: TextView) {
        imageView.setImageResource(animationId)
        imageView.startAnimation(animScaleKick)
        textView.setTextColor(0xFFFF8DA5.toInt())
        textView.typeface = Typeface.DEFAULT_BOLD
    }

    private fun reset(imageView: ImageView, resId: Int, textView: TextView) {
        imageView.setImageResource(resId)
        imageView.clearAnimation()
        textView.setTextColor(0xFFCECECE.toInt())
        textView.typeface = Typeface.DEFAULT
    }
}